﻿using MediatR;
using SmartTracking.API.Write.Application.Commands.User_command;
using SmartTracking.Domain.UserInfo;
using SmartTracking.ErrorCode;
using SmartTracking.Infrastructure.Interfaces;

namespace SmartTracking.API.Write.Application.CommandHandlers.User_handler
{
    public class DeleteUserCommandHandler:IRequestHandler<DeleteUserCommand,ApiResult<int>>
    {
        private readonly IBaseRepository<Users> userRep;

        public DeleteUserCommandHandler(IBaseRepository<Users> userRep)
        {
            this.userRep = userRep;
        }

        public async Task<ApiResult<int>> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ApiEnum.删除成功;
            result.Message = ApiEnum.删除成功.ToString();

            try
            {
                var list = userRep.GetAll().FirstOrDefault(x => x.UserId == request.UserId);
                list.IsDeleted = true;
                list.DeleteTime = DateTime.Now;
                var res = await userRep.UpdateAsync(list);

                result.data = res;
            }
            catch (Exception ex)
            {
                result.Code = ApiEnum.删除失败;
                result.Message = ApiEnum.删除失败.ToString();
                throw;
            }
            return await Task.FromResult(result);
        }
    }
}
